/*
 * Copyright (C) 2017 Pingan, Inc. All Rights Reserved.
 */

package com.pingan.haofang.searchcloud.api.facade.impl;

import com.pingan.haofang.searchcloud.api.CollectionManagerService;
import com.pingan.haofang.searchcloud.api.CollectionSummaryDto;
import com.pingan.haofang.searchcloud.api.DiskConditonDTO;
import com.pingan.haofang.searchcloud.api.StorageCluster;
import com.pingan.haofang.searchcloud.api.constants.EngineType;
import com.pingan.haofang.searchcloud.api.facade.CollectionManageFacade;
import com.pingan.haofang.searchcloud.api.matedata.CollectionMetaData;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.jmx.export.annotation.ManagedOperation;
import org.springframework.jmx.export.annotation.ManagedResource;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 搜索存储collection管理实现
 *
 * @author zhangbi617
 * @date 2017-07-17
 */
@Service
@ManagedResource
public class CollectionManageFacadeImpl implements CollectionManageFacade, ApplicationContextAware, InitializingBean {

    @Autowired
    private ApplicationContext applicationContext;

    private Map<EngineType, CollectionManagerService> collectionManagerMap = new HashMap<>();

    @Override
    public boolean create(StorageCluster storageCluster, CollectionMetaData collectionMetaData) throws Exception {
        return collectionManagerMap.get(storageCluster.getEngineType()).create(storageCluster.getClusterHost(),
                collectionMetaData);
    }

    @Override
    public boolean delete(StorageCluster storageCluster, String collectionName) throws Exception {
        return collectionManagerMap.get(storageCluster.getEngineType()).delete(storageCluster.getClusterHost(),
                collectionName);
    }

    @Override
    @ManagedOperation
    public List<String> listAllCollectionNames(StorageCluster storageCluster) throws Exception {
        return collectionManagerMap.get(storageCluster.getEngineType())
                .getAllCollectionNames(storageCluster.getClusterHost());
    }

    @Override
    public Map<String, CollectionSummaryDto> listAllCollectionSummary(StorageCluster storageCluster, List<CollectionMetaData> list) throws Exception {
        return collectionManagerMap.get(storageCluster.getEngineType())
                .getAllCollectionSummary(storageCluster.getClusterHost(), list);
    }

    /**
     * 获取集群中的存活的节点
     *
     * @param storageCluster 集群host
     * @return
     */
    @Override
    public List<String> getLiveHostUrl(StorageCluster storageCluster) {
        return collectionManagerMap.get(storageCluster.getEngineType()).getLiveHostUrl(storageCluster.getClusterHost());
    }


    /**
     * 获取集群中节点的统计信息
     *
     * @param storageCluster 集群host
     * @return
     */
    @Override
    public Map<String, DiskConditonDTO> getNodesSummary(StorageCluster storageCluster) throws Exception {
        return collectionManagerMap.get(storageCluster.getEngineType()).getNodesSummary(storageCluster.getClusterHost());
    }


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        Map<String, CollectionManagerService> map
                = this.applicationContext.getBeansOfType(CollectionManagerService.class);
        for (CollectionManagerService service : map.values()) {
            collectionManagerMap.put(service.getEngineType(), service);
        }
    }
}
